/**
 * 
 */
package com.alstudio.view.listener;

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

/**
 * 封装真对View的Touch事件。
 * 
 * @author alonso lee
 * 
 */
public class ALViewOnTouchManager implements OnTouchListener {

	private View view;
	private OnALTouchStateListener onALTouchStateListener;

	public ALViewOnTouchManager() {

	}

	/**
	 * 
	 */
	public ALViewOnTouchManager(View touchView, OnALTouchStateListener listener) {
		// TODO Auto-generated constructor stub
		this.view = touchView;
		this.onALTouchStateListener = listener;
		attach();
	}

	public void detach() {
		view.setOnTouchListener(null);
		this.onALTouchStateListener = null;
	}

	public void attach() {
		if (view != null) {
			view.setOnTouchListener(this);
		}
	}

	public void attach(View touchView, OnALTouchStateListener listener) {
		this.view = touchView;
		this.onALTouchStateListener = listener;
		attach();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnTouchListener#onTouch(android.view.View,
	 * android.view.MotionEvent)
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub

		if (event.getAction() == MotionEvent.ACTION_DOWN) {// 按下按钮不放
			if (onALTouchStateListener != null) {
				onALTouchStateListener.onTouchDown();
			}
			v.setPressed(true);
		} else if (event.getAction() == MotionEvent.ACTION_UP
				|| event.getAction() == MotionEvent.ACTION_CANCEL) {// 释放按钮
			if (onALTouchStateListener != null) {
				onALTouchStateListener.onTouchUp();
			}
			v.setPressed(false);
		} else if (event.getAction() == MotionEvent.ACTION_MOVE) {// 滑动按钮
			if (onALTouchStateListener != null) {
				onALTouchStateListener.onTouchMove();
			}
		}

		return true;

	}

	public interface OnALTouchStateListener {

		public void onTouchDown();

		public void onTouchUp();

		public void onTouchMove();

	}
}
